<?php
class MPayRequest extends MY_Model {

    protected $table = 'pay_request';
    private $filter = array();

    function __construct() {

        parent::__construct();
    }

    public function getAllShops(){
        $this->db->select( $this->table.'.*');
        $this->db->distinct();
        $this->db->from($this->table);
        if($this->_user->role_id == 2 || $this->_user->role_id == 4){
            $this->db->where('group_id',$this->_user->group_id);
        }

        $query = $this->db->get();
        $queryResult =  $query->result();
        $kvArr = array();
        foreach ($queryResult as $k=>$v){
            $kvArr[$v->id] = $v->name;
        }
        return $kvArr;
    }

    public static $requestStatus = Array(
        0 => '<span class="label label-warning">财务待处理</span>',
        1 => '<span class="label label-success">处理全部完成</span>',
        2 => '<span class="label label-warning">部分未处理完成</span>',
        3 => '<span class="label label-danger">申请记录信息有误</span>',
        4 => '<span class="label label-info">测评修改后重新提交</span>',
    );

    public static function getRStatus($method=''){
        if($method === ''){
            return self::$requestStatus;
        }
        return element($method, self::$requestStatus, '');
    }

    public function getList($limit, $sort=false){
        $this->db->select( '*');
        $this->db->distinct();
        $this->db->from($this->table);
        $this->db->limit($limit['limit'],$limit['offset']);
        $this->formfilter->doFilter();
        if(in_array($this->_user->role_id ,array(2,3,4,5))){   //销售测评组的人，只能看到自己
            $this->db->where('submiter_id',$this->_user->id);
        }
        if($this->_user->role_id == 8){   //财务组员的人，只能看到提交给自己的申请记录
            $this->db->where('finance_id',$this->_user->id);
        }
        if($sort){
            $this->db->order_by('sort', 'asc');
        }else{
            $this->db->order_by('id', 'desc');
        }
        $query = $this->db->get();
        return $query->result();
    }

    public function getListStatic(){
        $this->db->select( 'id');
        $this->db->distinct();
        $this->db->from($this->table);
        $this->formfilter->doFilter();
        if(in_array($this->_user->role_id ,array(2,3,4,5))){   //销售测评组的人，只能看到自己
            $this->db->where('submiter_id',$this->_user->id);
        }
        if($this->_user->role_id == 8){   //财务组员的人，只能看到提交给自己的申请记录
            $this->db->where('finance_id',$this->_user->id);
        }
        $query = $this->db->get();
        return $query->result();
    }

    public function getCount(){
        $this->db->select('count(distinct id) as num');
        $this->db->from($this->table);
        $this->formfilter->doFilter();
        if(in_array($this->_user->role_id ,array(2,3,4,5))){   //销售测评组的人，只能看到自己
            $this->db->where('submiter_id',$this->_user->id);
        }
        if($this->_user->role_id == 8){   //财务组员的人，只能看到提交给自己的申请记录
            $this->db->where('finance_id',$this->_user->id);
        }
        $tmp = $this->db->get()->result();
        return (int)$tmp[0]->num;
    }

    public function getById($id){
        $query = $this->db->where('id',$id)->get($this->table);
        return $query->row();
    }

    /**
     * @param array
     * @return object
     * excel 表格导出查询数据
     */
    public function exportList($where){

        $this->db->select("p.*,a1.account as sub_name,a2.account as fin_name,acc.name as acc_name,acc.account as acc_account");
        $this->db->from($this->table.' p');
        $this->db->join('admins a1', 'p.submiter_id = a1.id', 'LEFT');
        $this->db->join('admins a2', 'p.finance_id = a2.id', 'LEFT');
        $this->db->join('pay_account acc', 'p.pay_account = acc.id', 'LEFT');

        if ($begintime = $where['pay_request_create_time_begin']) {
            $this->db->where(array('p.create_time >= ', strtotime($begintime . ' 00:00:00')));
        }

        if ($endtime = $where['pay_request_create_time_end']) {
            $this->db->where(array('p.create_time <= ', strtotime($endtime . ' 23:59:59')));
        }

        if ($where['submiter_id']) {
            $this->db->where('p.submiter_id',$where['submiter_id']);
        }
        if ($where['coin_type']) {
            $this->db->where('p.coin_type',$where['coin_type']);
        }
        if ($where['status']) {
            $this->db->where('p.status',$where['status']);
        }
        if ($where['payment_type']) {
            $this->db->where('p.payment_type',$where['payment_type']);
        }

        if($this->_user->role_id == 3 || $this->_user->role_id == 5){   //测评组的人，只能看到自己
            $this->db->where('submiter_id',$this->_user->id);
        }
        if($this->_user->role_id == 7){   //测评组的人，只能看到自己
            $this->db->where('finance_id',$this->_user->id);
        }

        $result = $this->db->get()->result();

        foreach ($result as $k => $v){
            $v->pay_account   = $v->acc_name.'-'.$v->acc_account;
            $v->submiter_id   = $v->sub_name;
            $v->finance_id    = $v->fin_name;
            $v->status        = strip_tags($this->getRStatus($v->status));
            $v->coin_type     = IOSS_StaticConfig::getcoinTypeInfo($v->coin_type);
            $v->merge_method  = IOSS_StaticConfig::getMergeType($v->merge_method);
            $v->payment_type  = IOSS_StaticConfig::getPayTypeInfo($v->payment_type);
            $v->create_time   = date('Y-m-d H:i:s',$v->create_time);
        }
        return $result;
    }

    function changeFinance($rid,$fid){
        $sql = "update ".$this->db->dbprefix."pay_request set finance_id = ".$fid.' where id = '.$rid;
        return  $this->db->query($sql);
    }

}
